{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "if not any(path.endswith('textbook') for path in sys.path):\n",
    "    sys.path.append(os.path.abspath('../../..'))\n",
    "from textbook_utils import *"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(ch:risk)=\n",
    "# Model Selection"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So far when we fit models, we have used a few strategies to decide which features to include:\n",
    "\n",
    "+ Assess model fit with residual plots.\n",
    "+ Connect the statistical model to a physical model.\n",
    "+ Keep the model simple.\n",
    "+ Compare improvements in the standard deviation of the residuals and in the MSE between increasingly complex models. \n",
    "\n",
    "For example, when we examined the one-variable model of upward mobility in {numref}`Chapter %s <ch:linear>`, we found curvature in the residual plot. Adding a second variable greatly improved the fit in terms of average loss (MSE and, relatedly, multiple $R^2$), but some curvature remained in the residuals. A seven-variable model made little improvement over the two-variable model in terms of a decrease in MSE, so although the two-variable model still showed some patterns in the residuals, we opted for this simpler model.\n",
    "\n",
    "As another example, when we model the weight of a donkey in {numref}`Chapter %s <ch:donkey>`, we will take guidance from a physical model. We'll ignore the donkey's appendages and draw on the similarity between a barrel and a donkey's body to begin fitting a model that explains weight by its length and girth (comparable to a barrel's height and circumference). We'll then continue to adjust that model\n",
    "by adding categorical features related to the donkey's physical condition and age, collapsing categories, and excluding other possible features to keep the model simple. \n",
    "\n",
    "The decisions we make in building these models are based on judgment calls, and in \n",
    "this chapter we augment these with more formal criteria. \n",
    "To begin, we provide an example that shows why it's typically not a good idea to include too many features in a model. This phenomenon, called _overfitting_, often leads to models that follow the data too closely and capture some of the noise in the data. Then, when new observations come along, the predictions are worse than those from a simpler model. The remainder of the chapter provides techniques, such as the train-test split, cross-validation, and regularization, for limiting the impact of overfitting. These techniques are especially helpful when there are a large number of potential features to include in a model. We also provide a synthetic example, where we know the true model, to explain the concepts of model variance and bias and how they relate to over- and underfitting."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
